package com.mtw.bbs.article.controller.admin;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mtw.bbs.constant.TopicConstant;
import com.mtw.bbs.article.pojo.entity.TopicMember;
import com.mtw.bbs.article.pojo.form.TopicMemberForm;
import com.mtw.bbs.article.service.TopicMemberService;
import com.mtw.bbs.common.core.vo.Result;
import com.mtw.bbs.userAdmin.feign.UserClient;
import com.mtw.bbs.userAdmin.pojo.dto.UserDto;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 话题成员接口层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("admin/topicMember")
public class AdminTopicMemberController {


    private final UserClient userClient;
    private final TopicMemberService topicMemberService;







    /**
     * 加入话题
     */
    @PostMapping("/save")
    public Result joinTopic(@RequestBody TopicMemberForm form) {

        if (StringUtils.isBlank(form.getUsername())){
            return Result.fail("数据无效");
        }
        if (TopicConstant.TOPIC_MASTER.equals(form.getMemberType())){
            return Result.fail("数据无效");
        }
        Result<UserDto> userinfo = userClient.getUserInfoByUsername(form.getUsername());
        if (userinfo.isSuccess() && userinfo.getData() != null){
            boolean updated = topicMemberService.joinTopic(userinfo.getData().getId(),form.getTopicId(),form.getMemberType());
            return updated?Result.success():Result.fail("保存失败");
        }

        return Result.fail("数据无效");
    }



    /**
     * 变更话题题主
     */
    @PutMapping("/updateMaster")
    public Result updateMaster(@RequestBody TopicMemberForm form) {

        if (StringUtils.isBlank(form.getId())){
            return Result.fail("数据无效");
        }
        TopicMember member = topicMemberService.getById(form.getId());
        if (member == null){
            return Result.fail("数据不存在");
        }
        boolean result = topicMemberService.updateMaster(member.getUserId(), member.getTopicId());
        return result?Result.success():Result.fail("修改失败");
    }




    /**
     * 删除话题题主
     */
    @DeleteMapping("/deleteMaster")
    public Result deleteMaster(@RequestBody TopicMemberForm form) {

        if (StringUtils.isBlank(form.getId())){
            return Result.fail("数据无效");
        }
        TopicMember member = topicMemberService.getById(form.getId());
        if (member == null){
            return Result.fail("数据不存在");
        }
        boolean result = topicMemberService.deleteMaster(member.getUserId(), member.getTopicId());
        return result?Result.success():Result.fail("修改失败");
    }





    /**
     * 改变用户身份
     *
     * @param form 表单信息
     */
    @PutMapping("/changeType")
    public Result changeType(@RequestBody TopicMemberForm form) {

        if (StringUtils.isBlank(form.getId())){
            return Result.fail("数据无效");
        }
        TopicMember member = topicMemberService.getById(form.getId());
        if (member == null){
            return Result.fail("数据不存在");
        }
        boolean result = topicMemberService.updateMemberType(member.getUserId(), member.getTopicId(), form.getMemberType());
        return result?Result.success():Result.fail("修改失败");

    }





    @ApiOperation(value = "删除成员", notes = "根据ids来指定删除对象")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestBody TopicMemberForm form) {

        if (CollectionUtil.isEmpty(form.getDeleteIds())){
            return Result.fail("数据无效");
        }
        LambdaQueryWrapper<TopicMember> lqw = new LambdaQueryWrapper<>();
        lqw.select(TopicMember::getUserId,TopicMember::getTopicId);
        lqw.in(TopicMember::getId,form.getDeleteIds());
        List<TopicMember> list = topicMemberService.list(lqw);

        if (CollectionUtil.isEmpty(list)){
            return Result.success();
        }

        topicMemberService.deleteMember(list.stream().map(TopicMember::getUserId).toList(), list.get(0).getTopicId());
        return Result.success();
    }




}
